<template>
  <div class="search-container">
    <!-- 搜索框 -->
    <div class="search-header">
      <el-input
        v-model="searchKeyword"
        placeholder="请输入菜品名称"
        class="search-input"
        clearable
        @keyup.enter="handleSearch"
      >
        <template #append>
          <el-button icon="el-icon-search" @click="handleSearch" />
        </template>
      </el-input>
    </div>

    <!-- 搜索结果 -->
    <div class="search-results">
      <el-row :gutter="20">
        <el-col
          v-for="dish in dishList"
          :key="dish.id"
          :xs="24"
          :sm="12"
          :md="8"
          :lg="6"
          class="dish-item"
        >
          <el-card :body-style="{ padding: '0px' }" shadow="hover">
            <el-image
              :src="dish.image"
              fit="cover"
              class="dish-image"
              @click="viewDishDetail(dish.id)"
            />
            <div class="dish-info">
              <h3 class="dish-name">{{ dish.name }}</h3>
              <div class="dish-price">¥{{ dish.price }}</div>
              <div class="dish-shop">来自: {{ dish.shopName }}</div>
              <div class="dish-actions">
                <el-button
                  type="primary"
                  size="mini"
                  @click="viewDishDetail(dish.id)"
                >
                  查看详情
                </el-button>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>

      <!-- 分页 -->
      <div class="pagination-wrapper">
        <el-pagination
          :current-page="pagination.currentPage"
          :page-size="pagination.pageSize"
          :total="pagination.total"
          layout="prev, pager, next, jumper"
          @current-change="handlePageChange"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { searchDishes } from '@/api/dish'

export default {
  name: 'Search',
  data() {
    return {
      searchKeyword: '',
      dishList: [],
      pagination: {
        currentPage: 1,
        pageSize: 12,
        total: 0
      }
    }
  },
  created() {
    this.searchKeyword = this.$route.query.keyword || ''
    if (this.searchKeyword) {
      this.fetchDishes()
    }
  },
  methods: {
    handleSearch() {
      this.pagination.currentPage = 1
      this.fetchDishes()
    },
    async fetchDishes() {
      try {
        const params = {
          keyword: this.searchKeyword,
          page: this.pagination.currentPage,
          pageSize: this.pagination.pageSize
        }

        const response = await searchDishes(params)
        this.dishList = response.data.list
        this.pagination.total = response.data.total
      } catch (error) {
        console.error('搜索菜品失败:', error)
        this.$message.error('搜索菜品失败，请稍后重试')
      }
    },
    handlePageChange(page) {
      this.pagination.currentPage = page
      this.fetchDishes()
    },
    viewDishDetail(dishId) {
      this.$router.push(`/dishdetail/${dishId}`)
    }
  }
}
</script>

<style lang="scss" scoped>
.search-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;

  .search-header {
    margin-bottom: 20px;
    display: flex;
    justify-content: center;

    .search-input {
      width: 100%;
      max-width: 600px;
    }
  }

  .dish-item {
    margin-bottom: 20px;

    .dish-image {
      width: 100%;
      height: 180px;
      cursor: pointer;
      transition: transform 0.3s;

      &:hover {
        transform: scale(1.03);
      }
    }

    .dish-info {
      padding: 15px;

      .dish-name {
        margin: 0 0 10px;
        font-size: 16px;
        color: #333;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }

      .dish-price {
        color: #ff6600;
        font-weight: bold;
        font-size: 18px;
        margin-bottom: 5px;
      }

      .dish-shop {
        color: #666;
        font-size: 12px;
        margin-bottom: 10px;
      }

      .dish-actions {
        display: flex;
        justify-content: flex-end;
      }
    }
  }

  .pagination-wrapper {
    margin-top: 30px;
    display: flex;
    justify-content: center;
  }
}
</style>
